package sz.szsmk.citizencard.ui.adapter;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.seapeak.recyclebundle.BaseHolder;
import com.seapeak.recyclebundle.BaseRecyclerAdapter;

import java.util.List;

import sz.szsmk.citizencard.DaggerApplication;
import sz.szsmk.citizencard.R;
import sz.szsmk.citizencard.databinding.NoticeListItemBinding;
import sz.szsmk.citizencard.dialog.ListItemDelDialog;
import sz.szsmk.citizencard.model.network.bean.response.NoticeListRes;
import sz.szsmk.citizencard.ui.activity.NoticeListActivity;
import sz.szsmk.citizencard.ui.activity.YouzanActivity;
import sz.szsmk.citizencard.util.ACache;
import sz.szsmk.citizencard.util.Constant;
import sz.szsmk.citizencard.util.GlideRoundTransform;
import sz.szsmk.citizencard.util.JumpUtils;

/**
 * Copyright (c)2018
 * 欣网互联网络科技有限公司
 * author: zdh
 * created on: 2018/8/13 18:08
 * description:
 */
public class NoticeListAdapter extends BaseRecyclerAdapter<NoticeListAdapter.NotcieListHolder> {

    private NoticeListActivity mContext;
    private List<NoticeListRes.MessageDetailListBean> itemData;
    private TextView noticeDes;
    private TextView noticeTime;
    private TextView noticeTitle;
    private ImageView mImageiv;

    private static DelitemCallBack mDelItemCallBack;

    public NoticeListAdapter(NoticeListActivity noticeListActivity) {
        mContext = noticeListActivity;
    }


    public interface DelitemCallBack {
        void onDelItemCallBack(int position);
    }

    public void setDelItemCallBack(DelitemCallBack callBack) {
        mDelItemCallBack = callBack;
    }


//    @Override
//    public int getItemViewType(int position) {
//        return R.layout.notice_list_item;
//    }

    @Override
    public NotcieListHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return NotcieListHolder.creat(parent);
    }

    @Override
    public void onBindViewHolder(NotcieListHolder holder, int position) {
        loadItemView(position, holder.binding);
    }

    private void loadItemView(final int position, NoticeListItemBinding binding) {
        noticeDes = (TextView) binding.getRoot().findViewById(R.id.notice_list_des);
        noticeTime = (TextView) binding.getRoot().findViewById(R.id.notice_list_time);
        noticeTitle = (TextView) binding.getRoot().findViewById(R.id.notice_list_title);
        mImageiv = (ImageView) binding.getRoot().findViewById(R.id.notice_list_iv);

        if (itemData != null) {
            final NoticeListRes.MessageDetailListBean listBean = itemData.get(position);
            binding.noticeListDes.setText(listBean.getContent());
            String content = listBean.getContent();
            if (!TextUtils.isEmpty(content) && content.length() >= 8) {
                binding.noticeListMore.setVisibility(View.VISIBLE);
            } else {

                binding.noticeListMore.setVisibility(View.GONE);
            }
            binding.noticeListTime.setText(listBean.getCreateTime());
            binding.noticeListTitle.setText(listBean.getTitle());

//            ShowImageUtils.showImageViewToCircle(DaggerApplication.getIns(), R.drawable.zhanweitu_long, listBean.getImgUrl(), mImageiv);

            String imgUrl = listBean.getImgUrl();
            if (imgUrl == null) {
                mImageiv.setVisibility(View.GONE);
            }
            Glide.with(mContext)
                    .load(listBean.getImgUrl())
                    .error(R.drawable.zhanweitu_long)
                    .placeholder(R.drawable.zhanweitu_long)
                    .centerCrop()
                    .transform(new GlideRoundTransform(mContext))
                    .into(mImageiv);

            final String linkUrl = listBean.getLinkUrl();
            if (linkUrl == null || linkUrl.length() == 0) {
                binding.noticeListMore.setVisibility(View.GONE);
                return;
            }
            binding.noticeListMore.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Bundle bundle = new Bundle();
                    String linkUrl1 = itemData.get(position).getLinkUrl();

                    if (linkUrl1.equals("goDzSmkMainPage") || linkUrl1.equals("goDzSmkChargeDetail")) {
                        JumpUtils.jump(mContext, "dzsmk", 0);
                    } else if (linkUrl1.equals("toIdentityCodePage")) {
//                        toIdentityCodePage：到身份码页面
                        bundle.putString(Constant.ID_CARD, Constant.ID_CARD);
                        bundle.putString(Constant.ID_NAME, Constant.ID_NAME);
                        JumpUtils.jumptoNatvie(mContext, "identityCodeActivity", 0, bundle);
                    } else if (linkUrl1.equals("toCertificationPage")) {
//                        toIdentityCodePage：到实名认证页面
                        JumpUtils.jumptoNatvie(mContext, "RealNameActivity", 0, bundle);
                    } else if (linkUrl1.equals("toL2page")) {
//                        toIdentityCodePage：到L2认证页面
                        JumpUtils.jumptoNatvie(mContext, "RealNameActivity", 0, bundle);
                    } else if (linkUrl1.contains("youzan")) {
                        ACache.get(DaggerApplication.getIns()).remove("sykt");
                        ACache.get(DaggerApplication.getIns()).put("sykt", "sykt");

                        Intent intent = new Intent(mContext, YouzanActivity.class);
                        intent.putExtra(YouzanActivity.KEY_URL, linkUrl1);
                        mContext.startActivity(intent);
                    } else {
                        final String mUrl = linkUrl1 + "&access_token=" + ACache.get(mContext).getAsString("access_token");
                        if (mUrl.contains("card/spotRecordInfo")) {
                            bundle.putString(Constant.TITLE, "");
                        } else {
                            bundle.putString(Constant.TITLE, "");
                        }
                        bundle.putString("url", mUrl);
                        JumpUtils.jumptoNatvie(mContext, "webView", 0, bundle);
                    }
                }
            });

            binding.noticeItem.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {


                    final ListItemDelDialog itemDelDialog = new ListItemDelDialog(mContext, "删除");
                    itemDelDialog.show();
                    itemDelDialog.setOnOkClickListener(new ListItemDelDialog.OnOkClickListener() {
                        @Override
                        public void onOkClick() {
                            itemDelDialog.cancel();
                            if (mDelItemCallBack != null) {
                                mDelItemCallBack.onDelItemCallBack(position);
                            }
                        }
                    });

                    return true;
                }
            });
        }
    }

    @Override
    public int getItemCount() {
//        return 4;
        return null == itemData ? 0 : itemData.size();
    }

    public void setItemData(List<NoticeListRes.MessageDetailListBean> itemData) {
        this.itemData = itemData;
    }

    public static class NotcieListHolder extends BaseHolder {
        private NoticeListItemBinding binding;

        public static NotcieListHolder creat(ViewGroup parent) {
            NoticeListItemBinding binding = NoticeListItemBinding
                    .inflate(LayoutInflater.from(parent.getContext()), parent, false);
            return new NotcieListHolder(binding);
        }
//        public static NotcieListHolder creat(ViewGroup parent){
//            NoticeListItemBindingListItemBinding.inflate(LayoutInflater.from())

        //        }
        public NotcieListHolder(NoticeListItemBinding binding) {
            super(binding.getRoot());
            this.binding = binding;

        }
    }
}
